 package dlb.busstop.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import dlb.busstop.R;
import dlb.busstop.data.BusStop;

public class StopListViewAdapter extends BaseAdapter{

	private Activity mActivity;
	private List<BusStop> mItems = new ArrayList<BusStop>();
	private LayoutInflater mLayoutInflater;
	
	public StopListViewAdapter(Activity activity) {
		mActivity = activity;
		mLayoutInflater = activity.getLayoutInflater();
	}

	public List<BusStop> getList() {
		return mItems;
	}
	
	public synchronized void setList(List<BusStop> items) {
		
		this.mItems = items;
		sortByDistance();
		notifyDataSetChanged();
	}
	
	public synchronized void clearItems(){
		
		mItems.clear();
		notifyDataSetChanged();
	}
	
	public synchronized void addItem(BusStop item){
		
		mItems.add(item);
		sortByDistance();
		notifyDataSetChanged();
	}
	
	public synchronized void removeItem(int position){
		
		mItems.remove(position);
		sortByDistance();
		notifyDataSetChanged();
	}
	
	public synchronized void removeItem(BusStop item){
		
		mItems.remove(item);
		sortByDistance();
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return mItems.size();
	}

	@Override
	public BusStop getItem(int position) {
		return mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return mItems.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null)
			convertView = mLayoutInflater.inflate(R.layout.test_bus_stop_item, parent, false);
		
		TextView distance = (TextView) convertView.findViewById(R.id.tv_distance);
		TextView description = (TextView) convertView.findViewById(R.id.tv_stopAddress);
		TextView buses = (TextView) convertView.findViewById(R.id.tv_busNumbers);
		
		BusStop currentObj = mItems.get(position);
		currentObj.setBuses("41, 44, 45, 150, 21, 31, ...");
		
		
		distance.setText(calculateDistance(currentObj.getDistance()));
		description.setText(currentObj.toString());
		buses.setText(currentObj.getBuses());
		
		return convertView;
	}
	
	public String calculateDistance(float distance){
		
		String toBeReturned = "";
		if(distance  < 1) {
			int inMeters = (int) (distance * 1000);
			toBeReturned = String.valueOf(inMeters) + " " + mActivity.getString(R.string.M);
			return toBeReturned;
		}
		else{
			toBeReturned = String.format("%.1f", distance)+ " " + mActivity.getString(R.string.Km);
			return toBeReturned;
		}
	}
	
	public void sortByDistance(){
		
		Comparator<BusStop> comparator = new Comparator<BusStop>() {
			
			@Override
			public int compare(BusStop lhs, BusStop rhs) {
				return lhs.getDistance() > rhs.getDistance() ? 1 : -1;
			}
		};
		Collections.sort(mItems, comparator);
	}
}
